<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>

<?php
class Useronline {
    var $username;
    var $time;
    var $session_id;
    
    function Useronline()
    {
        $this->CI = &get_instance();
        $this->CI->load->library('session');
        $this->username = 'Guest';
        $this->time = time();
        $this->session_id = $this->CI->session->userdata('session_id');
        if($this->checkOnline())
        {
            $this->CI->db->where('session_id', $this->session_id);
            $this->CI->db->update('user_online', array('time' => $this->time));
        }
        else
        {
            $data = array('tendangnhap' => $this->username, 'session_id' => $this->session_id, 'time' => $this->time);
            $this->CI->db->insert('user_online', $data);
        }
    }
    
    function countOnline()
    {
        $count = $this->CI->db->get('user_online');
        return $count->num_rows();
    }
    
    function checkOnline()
    {
        $this->CI->db->where('session_id', $this->session_id);
        $this->CI->db->where('time',($this->time - $this->time) > 300);
        $query = $this->CI->db->get('user_online');
        if($query) return true; else return false;
    }
    
    function listOnline()
    {
        
    }
}
?>